<template>
  <div><UploadExcel :on-success="handleSuccess" /></div>
</template>

<script>
import UploadExcel from '@/components/UploadExcel/index.vue'
import { formatExcelDate } from '@/utils/index.js'
import { importEmployee } from '@/api/employees.js'
export default {
  name: 'Import',
  components: {
    UploadExcel
  },
  methods: {
    formdata(header, results) {
      const mapInfo = {
        '入职日期': 'timeOfEntry',
        '手机号': 'mobile',
        '姓名': 'username',
        '转正日期': 'correctionTime',
        '工号': 'workNumber',
        '部门': 'departmentName',
        '聘用形式': 'formOfEmployment'
      }
      //  results excel表格的内容
      // // [
      //     {'姓名'：'小张'， '手机号': '13712345678'}
      //   , {.....}
      //   ]
      const list = results.map(obj => {
        const newObj = {}
        Object.keys(obj).forEach(zhkey => {
          const enkey = mapInfo[zhkey]
          if (enkey === 'timeOfEntry' || enkey === 'correctionTime') {
            // 后端需要的日期格式是标准时间
            newObj[enkey] = new Date(formatExcelDate(obj[zhkey]))
          } else {
            newObj[enkey] = obj[zhkey]
          }
        })
        return newObj
      })
      return list
    },
    handleSuccess({ header, results }) {
      const data = this.formdata(header, results)
      // console.log(data)
      this.doImport(data)
    },
    async doImport(data) {
      await importEmployee(data)
      // 页面后退
      this.$router.back()
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
